//
//  NSString+Categories.m
//  BaseProject
//
//  Created by Guicai_Li on 3/28/14.
//  Copyright (c) 2014 Guicai_Li. All rights reserved.
//

#import "NSString+Categories.h"
#import <CommonCrypto/CommonDigest.h>

@implementation NSString (Categories)

/**
 *  计算 MD5 值
 *
 *  @param input
 *
 *  @return
 */
+ (NSString *) md5:(NSString *) input
{
//    const char *cStr = [input UTF8String];
//    unsigned char digest[16];
//    CC_MD5( cStr, strlen(cStr), digest );
//    
//    NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
//    
//    for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++){
//        [output appendFormat:@"%02x", digest[i]];
//    }
//    return  output;
    

        const char *cStr = [input UTF8String];
        unsigned char result[16];
        CC_MD5(cStr, strlen(cStr), result); // This is the md5 call
    NSString *md5Str = [NSString stringWithFormat:
                        @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
                        result[0], result[1], result[2], result[3],
                        result[4], result[5], result[6], result[7],
                        result[8], result[9], result[10], result[11],
                        result[12], result[13], result[14], result[15]
                        ]; 
    return md5Str;
//    return [md5Str substringWithRange:NSMakeRange(8, 16)];
    
}

/**
 *
 *
 *  @param trim 是否要删除字符串中的空格
 *
 */
+ (BOOL)isEmpty:(NSString *)str trim:(BOOL)trim{
    if(str == nil || str.length == 0){
        return YES;
    }else{
        if(trim){
            return [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]].length == 0;
        }
    }
    return NO;
}

- (NSString *)urlEncode {
    return [[self stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
}




/**
 @method 获取指定宽度情况ixa，字符串value的高度
 @param value 待计算的字符串
 @param fontSize 字体的大小
 @param andWidth 限制字符串显示区域的宽度
 @result float 返回的高度
 */
+ (float) heightForString:(NSString *)str fontSize:(float)fontSize andWidth:(float)width
{
    CGRect rect = [str boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:fontSize]} context:Nil];
    //此处的换行类型（lineBreakMode）可根据自己的实际情况进行设置
    
    return ceilf(rect.size.height);
}


/**
 @method 获取指定宽度情况ixa，字符串value的高度
 @param value 待计算的字符串
 @param fontSize 字体的大小
 @param andheight 限制字符串显示区域的宽度
 @result float 返回的高度
 */
+ (float) widthForString:(NSString *)str fontSize:(float)fontSize andheight:(float)height
{
    CGRect rect = [str boundingRectWithSize:CGSizeMake(MAXFLOAT, height) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:fontSize]} context:Nil];
    //此处的换行类型（lineBreakMode）可根据自己的实际情况进行设置
    return ceilf(rect.size.width);
}






@end
